<template>
  <div class="login-container">
    <el-form :model="loginForm" :rules="loginRules" ref="loginForm" label-width="100px">
      <h3 class="login-title" v-text="appName"></h3>
      <el-form-item prop="username">
        <el-input
          placeholder="请输入手机号码"
          type="tel"
          :maxlength="11"
          :minlength="11"
          size="large"
          required
          v-model="loginForm.username"
        >
          <template slot="prepend">账号</template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          placeholder="请输入密码"
          size="large"
          type="password"
          @keyup.native.enter="loginSubmit('loginForm')"
          v-model="loginForm.password"
        >
          <template slot="prepend">密码</template>
        </el-input>
      </el-form-item>
      <div class="login-form login-btngroup">
        <el-button type="primary" size="large" @click="loginSubmit('loginForm')">登录</el-button>
        <el-button size="large" @click="resetForm('loginForm')">重置</el-button>
      </div>
    </el-form>
  </div>
</template>
<script>
import * as utils from "@/utils/comm";
import { login } from "@/api/demo/user";

export default {
  data() {
    return {
      appName: "欢迎登录",
      loginForm: {
        username: "",
        password: ""
      },
      loginRules: {
        username: [
          { required: true, message: "手机号码不能为空", trigger: "change" },
          {
            min: 11,
            max: 11,
            message: "手机号码必须为11位",
            trigger: "change"
          },
          {
            pattern: utils.telPattern,
            message: "请输入正确的手机号码",
            trigger: "change"
          }
        ],
        password: [
          {
            required: true,
            message: "密码不允许为空，请输入密码",
            trigger: "change"
          }
        ]
      }
    };
  },
  computed: {},
  methods: {
    loginSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          login(this.loginForm).then(
            res => {
              if (res.status >= 200 && res.status <= 299) {
                console.log("用户登录成功", res);
                if (res.data.code === 0) {
                  this.$router.push({ path: "dashboard" });
                }
              }
            },
            error => {
              console.log("用户登录失败===>", error.response);
            }
          );
        } else {
          this.$notify.error({
            title: "错误",
            message: "表单校验失败"
          });
        }
      });
    },
    resetForm(formName) {
      this.loginForm = { username: "", password: "" };
      this.$refs[formName].resetFields();
    }
  },
  components: {}
};
</script>

<style lang="less">
@import "@/styles/variable.less";
.login-container {
  position: relative;
  width: 100%;
  height: 100%;
  line-height: 100%;
  min-height: 100%;
  display: flex;
  margin: 0;
  background-size: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .el-form {
    width: 360px;
    min-width: 360px;
    padding: 15px 25px 35px;
    z-index: 10;
    background-color: rgba(255, 255, 255, 0.5);
    box-shadow: 0 0 25px #cac6c6;
    .el-form-item__content {
      margin-left: 0 !important;
    }
    .login-title {
      text-align: center;
      padding: 15px 0;
      margin-bottom: 35px;
      font-weight: bold;
      font-size: 24px;
    }
    .login-form {
      margin-bottom: 25px;
    }
    .login-btngroup {
      .el-button {
        width: 47.8%;
      }
    }
  }
}
</style>
